home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Programming / C Reference Card 3.0 / C Reference Card / C Reference Card.rsrc / TEXT_405_5.txt < prev    next >
Encoding:
Text File  |  1997-07-17  |  5.9 KB  |  246 lines

  1. FUNCTIONS : basics, overloading, command line arguments, C and C++, ...
  2. _________________________________________________________________________
  3.  
  4.  
  5. FUNCTION BASICS
  6.  
  7. Almost all programs use functions to break-up large programs into manageable and reusable chunks of code.  Related functions are usually grouped into separate files which are independently compiled.  When accessing global variables from a separate file or library, you must use the "extern" keyword followed by the variable declaration to tell the compiler that the particular variable is not part of the file.  The following is an example of how to call functions from a separate file with a shared global variable:
  8.  
  9.   // file1.cp
  10.   #include <iostream.h>
  11.  
  12.   // global variable in external file
  13.   extern float result;
  14.  
  15.   // function prototypes (external)
  16.   float myInput( char *prompt );
  17.   void myMultiply( float a, float b );
  18.   void myPrintResult( float result );
  19.  
  20.   main()
  21.   {
  22.     float a,b;
  23.  
  24.     a = myInput( "Input A: " );
  25.     b = myInput( "Input B: " );
  26.     myMultiply( a, b );
  27.     myPrintResult( result );
  28.  
  29.     return 0;
  30.   }
  31.   // end file1.cp
  32.  
  33.  
  34.   // file2.cp
  35.   #include <iostream.h>
  36.  
  37.   float result;
  38.  
  39.   // function prototypes (local)
  40.   float myInput( char *prompt );
  41.   void myMultiply( float a, float b );
  42.   void myPrintResult( float result );
  43.  
  44.   float myInput( char *prompt )
  45.   {
  46.     float input;
  47.  
  48.     cout << prompt;
  49.     cin >> input;
  50.   
  51.     return input;
  52.   }
  53.  
  54.   void myMultiply( float a, float b )
  55.   {
  56.     result = a * b;
  57.   }
  58.  
  59.   void myPrintResult( float result )
  60.   {
  61.     cout << "The result is ";
  62.     cout << result << endl;
  63.   }
  64.   // end file2.cp
  65.  
  66. Although not required, you can insert the extern keyword in front of the function prototypes in 'file1.cp' to identify that these functions are located in an external file.  Instead of duplicating the function prototypes in both files as done above, you could place these prototypes in a separate header file, preferably called 'file2.h', and #include this file at the beginning of 'file1.cp' and 'file2.cp'.
  67.  
  68.  
  69. FUNCTION OVERLOADING
  70.  
  71. In C++, you can create more than one function with the same name.  However, the parameter list of these "overloaded" functions must differ.  When asked to call a function, the compiler will compare the parameters of the called function to the list of functions which share the same name.  When a match is determined, the proper function is called.
  72.  
  73.   // overload.cp
  74.   #include <iostream.h>
  75.  
  76.   void DoAdd( int a, int b );
  77.   void DoAdd( float a, float b );
  78.  
  79.   main()
  80.   {
  81.     int i1   =   5;
  82.     int i2   =  10;
  83.     float f1 = 2.5;
  84.     float f2 = 4.1;
  85.  
  86.     DoAdd( i1, i2 );
  87.     DoAdd( f1, f2 );
  88.  
  89.     return 0;
  90.   }
  91.  
  92.   void DoAdd( int a, int b )
  93.   {
  94.     int result;
  95.  
  96.     result = a + b;
  97.     cout << a << " + " << b << " = " << result << endl;
  98.   }
  99.  
  100.   void DoAdd( float a, float b )
  101.   {
  102.     float result;
  103.  
  104.     result = a + b;
  105.     cout << a << " + " << b << " = " << result << endl;
  106.   }
  107.   // end overload.cp
  108.  
  109.  
  110.  
  111. COMMAND LINE ARGUMENTS
  112.  
  113. Although the Macintosh doesn't utilize a command line prompt (at least not yet), you may need to write programs which use command line arguments.  The following code should work with most Macintosh compilers:
  114.  
  115.   // command.cp
  116.   #include <iostream.h>
  117.   #include <console.h>                // compiler-specific
  118.  
  119.   int main( int argc, char *argv[] )  // full prototype for main
  120.   {
  121.     int i;
  122.  
  123.     argc = ccommand( &argv );         // compiler-specific
  124.  
  125.     for( i = 1; i < argc; i++ )
  126.       cout << argv[i] << " ";
  127.     cout << endl;
  128.  
  129.     return 0;
  130.   }
  131.   // end command.cp
  132.  
  133.  
  134.  
  135. CALLING C FUNCTIONS FROM C++
  136.  
  137. C++ performs "name mangling" in order to perform such feats as function overloading.  Basically, this means that C++ renames your functions behind the scenes.  Therefore, if you need to call a C function (this usually means calling a function from an object code library which has been compiled by a straight C compiler), you need to let the C++ compiler know not to perform any name mangling.  This is performed by using the following statement:
  138.  
  139.   extern "C" function_prototype;
  140.  
  141. If there is a list of functions, you can use:
  142.  
  143.   extern "C"
  144.   {
  145.     function_prototype1;
  146.     function_prototype2;
  147.     function_prototype3;
  148.     //...
  149.   }
  150.  
  151. To enclose a complete library, you can use:
  152.  
  153.   extern "C"
  154.   {
  155.     #include <header_file.h>
  156.   }
  157.  
  158.  
  159.  
  160. DEFAULT ARGUMENT LIST
  161.  
  162. You can supply default values for function arguments in C++ as follows:
  163.  
  164.   // default.cp
  165.   #include <iostream.h>
  166.  
  167.   void myPrint( int val = 0, int times = 1 );
  168.  
  169.   main()
  170.   {
  171.     myPrint();
  172.     myPrint(5);
  173.     myPrint(7,3);
  174.  
  175.     return 0;
  176.   }
  177.  
  178.   void myPrint( int val, int times )
  179.   {
  180.     for( int i=1; i<=times; i++ )
  181.     {
  182.       if( i==1 )
  183.         cout << "Output Value: " << val << endl;
  184.       else
  185.         cout << "      Copy " << i << ": " << val << endl;
  186.     }
  187.   }
  188.   // end default.cp
  189.  
  190.  
  191.  
  192. VARIABLE-LENGTH ARGUMENT LIST
  193.  
  194. Although not recommended, you may need to create a function which has a variable argument list (such as printf and scanf).  Here's how to define and use variable-length argument lists in functions:
  195.  
  196.   // list.cp
  197.   #include <iostream.h>
  198.   #include <stdarg.h>
  199.  
  200.   void myPrintList( char *format, ... );  // ellipsis must be last item
  201.  
  202.   main()
  203.   {
  204.     int    int1  = 2;
  205.     int    int2  = 7;
  206.     double real1 = 3.14;
  207.   
  208.     myPrintList( "List:%d%d%f", int1, int2, real1 );
  209.  
  210.     return 0;
  211.   }
  212.  
  213.   void myPrintList( char *format, ... )
  214.   {
  215.     va_list argPtr;
  216.     char    *p;
  217.     int     intItem;
  218.     double  realItem;
  219.  
  220.     va_start( argPtr, format );
  221.     for( p = format; *p; p++ )  // search for end of string
  222.     {
  223.       if( *p != '%' )
  224.       {
  225.         cout << *p; // print char
  226.         continue;   // get next letter
  227.       }
  228.       cout << endl;
  229.       switch( *++p )
  230.       {
  231.         case 'd':
  232.           intItem = va_arg( argPtr, int );
  233.           cout << intItem;
  234.           break;
  235.         case 'f':
  236.           realItem = va_arg( argPtr, double );
  237.           cout << realItem;
  238.           break;
  239.         default:
  240.           cout << "%" << *p;
  241.           break;
  242.       }
  243.     }
  244.     va_end( argPtr ); // clean up
  245.   }
  246.   // end list.cp